/*
 * IdentifierFactory.java    0.0.1    28/set/2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.core.identifiers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This is a class used as a factory for identifiers. It provides only one static method 
 * used to obtain a new {@link org.mentalsmash.tazio.core.identifiers.TazioID TazioID} 
 * object which will uniquely identify the object it is associated to.
 * <p>
 * This factory also contains the field factoryClass used to hold the Class of the 
 * implementation of 
 * {@link org.mentalsmash.tazio.core.identifiers.IdentifiersFactoryImpl IdentifierFacoryImpl} 
 * which will actually create the identifier. This usage of the 
 * {@link org.mentalsmash.tazio.core.identifiers.IdentifiersFactoryImpl IdentifierFacoryImpl}
 * interface is done for reusability purposes.
 * </p>
 * 
 * @version 0.0.1	28/set/2009
 * @author Enrico Savioli <savioli.enrico@gmail.com>
 * 
 * @see TazioID
 */
public class IdentifiersFactory {

	private final static Logger log = LoggerFactory.getLogger(IdentifiersFactory.class);
	
	private static Class<? extends IdentifiersFactoryImpl> factoryClass = org.mentalsmash.tazio.core.identifiers.UUIDIdentifiersFactoryImpl.class;
	
	private static IdentifiersFactoryImpl factory = null;
	
	/**
	 * This method calls the newID method of the 
	 * {@link IdentifiersFactoryImpl IdentifierFacoryImpl} which is currently used in the 
	 * system. The first time that this method is used, the factory field will be null, thus 
	 * causing the call of 
	 * {@link org.mentalsmash.tazio.core.identifiers.IdentifiersFactory#initFactory() initFactory()}
	 * 
	 * @return the newly created TazioID
	 */
	public static TazioID newIdentifier(String symbolicName){
		
		if (factory == null) {
			initFactory();
		}
		
		return factory.newID();
	}

	/**
	 * This private method is used the first time the 
	 * {@link IdentifiersFactory#newIdentifier() newIdentifier()} method is invoked, it
	 * instantiates a new object implementing the {@link IdentifiersFactoryImpl} 
	 * interface held in the <code>facoryClass</code> field and stores it in the 
	 * <code>factory</code> field.
	 */
	private static void initFactory() {
		
		try {

			factory= factoryClass.newInstance();
					
		} catch (InstantiationException e) {
			String msg = "Error occurred while creating the factory through reflection";
			log.error(msg);
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			String msg = "Illegal access occurred while creating the factory through reflection";
			log.error(msg);
			e.printStackTrace();
		}
		
	}
	
}
